<!--
 * @Author: Olivia
 * @Date: 2022-05-26 22:41:15
 * @LastEditTime: 2022-08-23 21:41:13
 * @FilePath: \toutiao-m\src\views\home\index.vue
 * @custom_string_Olivia: Less is more
-->
<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        round
        icon="search"
        type="info"
        size="small"
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable>
      <van-tab
        v-for="item in channels"
        :key="item.id"
        class="channel-tabs"
        :title="item.name"
        :style="{backgroundColor: item.color}"
      >
        <div class="banner">
          <ArticleList :item="item" />
        </div>
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChannelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑 -->
    <van-popup
        v-model="isChannelEditShow"
        close-icon-position="top-left"
        closeable
        position="bottom"
        :style="{ height: '100%' }"
    >
    <channelEdit :my-channels="channels" :active="active" @update-active= "onUpdateActice"/>
    </van-popup>
    <!-- 频道编辑 -->
  </div>
</template>
<script>
import { getUserChannels } from '../../api/user'
import ArticleList from './components/article-list'
import channelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '../../utils/storage'
export default {
  name: '',
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  created () {
    this.loadChannels()
    this.$nextTick(() => {
      document.querySelector('.hamburger-btn').style =
        'position: fixed; right: 0; width: 33px; height: 41px; display: flex;align-items: center; justify-content: center; background-color: #fff; opacity:0.902;'
    })
  },
  components: { ArticleList, channelEdit },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          // 登录状态
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录状态
          const localChannels = getItem('TOTIAO_CHANNELS') || []
          //   判断是否有本地的频道数据
          if (localChannels) {
            // 有，拿来用
            channels = localChannels
          } else {
            // 没有，默认加载频道
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (error) {
        this.$toast('用户频道列表加载失败')
      }
    },
    onUpdateActice (index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  padding-bottom: 50px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }

  .search-btn {
    width: 555px/2;
    height: 64px/2;
    background-color: #5babfb;
    border: none;
    font-size: 28px/2;
    .van-icon {
      font-size: 32px/2;
    }
  }
  .banner {
    height: 758px;
    // overflow: scroll;
    overflow-y: auto;
  }
    /deep/.channel-tabs {
    .van-tabs__nav {
      padding-bottom: 0;
    }
    /deep/.van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      width: 100%;
      padding-right: 12px;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px/2;
      font-size: 15px;
      color: #777777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__line {
      width: 31px/2;
      height: 8px/2;
      bottom: 4px;
    }
    .placeholder {
      width: 33px;
      height: 41px;
      //   是flex不生效
      flex-shrink: 0;
    }
    #hamburger-btn {
      position: fixed;
      right: 0;
      width: 33px;
      height: 41px;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px/2;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px/2;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
/deep/.van-tabs__nav{
    padding-right: 20px;
    }
}
</style>
